package com.hanma.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hanma.bean.Category;
import com.hanma.bean.User;
import com.hanma.service.CategoryService;
import com.hanma.service.impl.CategoryServiceImpl;

@WebServlet({"/cateadd.do","/catelist.do","/cateaddAction.do","/cateupdate.do","/cateupdateAction.do","/catedelete.do"})
public class CategoryServlet extends HttpServlet{
	//后端新闻分类管理
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String path = req.getServletPath();
		req.setCharacterEncoding("utf-8");
		CategoryService service = new CategoryServiceImpl();
		if(path.equals("/catelist.do")) {
			Integer pid = 0;
			Category cate = null;
			if(req.getParameter("pid") != null) {
				pid = Integer.parseInt(req.getParameter("pid"));
				cate = service.find(pid);
			}
			List<Category> cateList = service.findAll(pid);
			req.setAttribute("pid",pid);
			req.setAttribute("cate",cate);
			req.setAttribute("cateList",cateList);
			req.getRequestDispatcher("/manage/category/list.jsp").forward(req, resp);
		}else if(path.equals("/cateadd.do")) {
			Integer pid = Integer.parseInt(req.getParameter("pid"));
			req.setAttribute("pid",pid);
			List<Category> cateList = null;
			if(pid > 0) {
				Category cate = service.find(pid);
				cateList = service.findAll(cate.getPid());
			}
			req.setAttribute("cateList",cateList);
			req.getRequestDispatcher("manage/category/add.jsp").forward(req, resp);
		}else if(path.equals("/cateaddAction.do")) {
			String name = req.getParameter("name");
			Integer pid = 0;
			if(req.getParameter("pid") != null) {
				pid = Integer.parseInt(req.getParameter("pid"));
			}
			Category cate = new Category(name,pid);
			service.insert(cate);
			resp.sendRedirect("catelist.do");
		}else if(path.equals("/cateupdate.do")) {
			Integer id = Integer.parseInt(req.getParameter("id"));
			Category cate = service.find(id);
			List<Category> cateList = null;
			if(cate.getPid() > 0) {
				Category cate1 = service.find(cate.getPid());
				cateList = service.findAll(cate1.getPid());
			}
			req.setAttribute("cateList",cateList);
			req.setAttribute("cate", cate);
			req.getRequestDispatcher("manage/category/update.jsp").forward(req, resp);
		}else if(path.equals("/cateupdateAction.do")) {
			String name = req.getParameter("name");
			Integer id = Integer.parseInt(req.getParameter("id"));
			Integer pid = Integer.parseInt(req.getParameter("pid"));
			Category cate = new Category(id,name,pid);
			service.update(cate);
			resp.sendRedirect("catelist.do");
		}else if(path.equals("/catedelete.do")) {
			Integer id = Integer.parseInt(req.getParameter("id"));
			service.delete(id);
			resp.sendRedirect("catelist.do");
		}
	}

}
